﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using InstantNaukriDAL;
using System.Collections.Generic;

public partial class SubBranchMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                clsSubBranchMasterDAL objclsSubBranchMasterDAL = new clsSubBranchMasterDAL();
                FillSubBranch();
                //FillddlState();
                FillddlBranch();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

    protected void btnsaveEmployeinfo_Click(object sender, EventArgs e)
    {
        try
        {
            if (Session["SubBranchId"] == null)
            {
                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                //Saving User
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                objIN_TB_USER.EMAIL = txtemail.Text;

                if (rbtnLoginId.SelectedValue == "1")
                {
                    objIN_TB_USER.LOGIN_ID = txtemail.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }
                if (rbtnLoginId.SelectedValue == "2")
                {
                    objIN_TB_USER.LOGIN_ID = txtmobile.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }
                objIN_TB_USER.NAME = txtSubBranchName.Text;
                objIN_TB_USER.MOBILE = txtmobile.Text;
                objIN_TB_USER.EMAIL = txtemail.Text;
                objIN_TB_USER.STATUS = 1;
                objIN_TB_USER.ROLEID = 6;
                objIN_TB_USER.DOB = Convert.ToDateTime(txtDob.Text);
                objIN_TB_USER.CREATED_BY = 1;
                int UserId = objclsUserLoginDAL.SaveUser(objIN_TB_USER);
                
                clsSubBranchMasterDAL objclsSubBranchMasterDAL = new clsSubBranchMasterDAL();
                IN_TB_SUBBRANCH_MST objIN_TB_SUBBRANCH_MST = new IN_TB_SUBBRANCH_MST();
                objIN_TB_SUBBRANCH_MST.USER_ID = UserId;
                objIN_TB_SUBBRANCH_MST.BRANCH_ID = Convert.ToInt32(ddlBranch.SelectedValue);
                objIN_TB_SUBBRANCH_MST.SUBBRANCH_NAME = txtSubBranchName.Text;
                objIN_TB_SUBBRANCH_MST.SUBBRANCH_DESCRIPTION = txtSubBranchDescription.Text;
                objIN_TB_SUBBRANCH_MST.LOGIN_TILL = Convert.ToDateTime(txtlogintill.Text.ToString());
                objIN_TB_SUBBRANCH_MST.DOWNLOAD_TILL = Convert.ToDateTime(txtdownloadtill.Text);
                objIN_TB_SUBBRANCH_MST.MOBILE = txtmobile.Text;
                objIN_TB_SUBBRANCH_MST.EMAIL = txtemail.Text;
                objIN_TB_SUBBRANCH_MST.STATUS = 1;
                int SubBranchId = objclsSubBranchMasterDAL.SaveSubBranchMst(objIN_TB_SUBBRANCH_MST);
                //ClearAllControls();
                FillSubBranch();
            }
            else
            {
                 clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                 IN_TB_USER ObjIN_TB_USER = new IN_TB_USER();

                 ObjIN_TB_USER = objclsUserLoginDAL.GetUserBySubBranchID(Convert.ToInt32(Session["SubBranchId"].ToString()));
                 if (ObjIN_TB_USER != null)
                {
                    clsSubBranchMasterDAL objclsSubBranchMasterDAL = new clsSubBranchMasterDAL();
                    IN_TB_SUBBRANCH_MST objIN_TB_SUBBRANCH_MST = new IN_TB_SUBBRANCH_MST();
                    objIN_TB_SUBBRANCH_MST.ID = Convert.ToInt32(Session["SubBranchId"].ToString());
                    objIN_TB_SUBBRANCH_MST.SUBBRANCH_NAME = txtSubBranchName.Text.Trim();
                    objIN_TB_SUBBRANCH_MST.PHONE = txtPhone.Text;
                    objIN_TB_SUBBRANCH_MST.MOBILE = txtmobile.Text;
                    objIN_TB_SUBBRANCH_MST.USER_ID = ObjIN_TB_USER.ID;
                      objIN_TB_SUBBRANCH_MST.BRANCH_ID = Convert.ToInt32(ddlBranch.SelectedValue);
                    objIN_TB_SUBBRANCH_MST.SUBBRANCH_DESCRIPTION = txtSubBranchDescription.Text;
                    objIN_TB_SUBBRANCH_MST.DOWNLOAD_TILL = Convert.ToDateTime(txtdownloadtill.Text);
                    objIN_TB_SUBBRANCH_MST.LOGIN_TILL = Convert.ToDateTime(txtlogintill.Text);
                    int SubBranchId = objclsSubBranchMasterDAL.SaveSubBranchMst(objIN_TB_SUBBRANCH_MST);
                }
                FillSubBranch();
                //ClearAllControls();
                Session["SubBranchId"] = null;
            }
        }

        catch (Exception)
        {
            throw;
        }




    }
    protected void FillSubBranch()
    {
        try
        {
            List<clsGetSubBranchInfo> objIN_TB_SUBBRANCH_MSTist = new List<clsGetSubBranchInfo>();
            clsSubBranchMasterDAL objclsSubBranchMasterDAL = new clsSubBranchMasterDAL();

            //objIN_TB_EMPLOYER_MSTist = objclsEmployeDAL.GetAllEmployerFullInfo();
            objIN_TB_SUBBRANCH_MSTist = objclsSubBranchMasterDAL.GetAllSubBranchFullInfo();
            if (clsEncryptionDecryption.Decrypt(Session["RoleId"].ToString()).Trim() == "5")
            {
                objIN_TB_SUBBRANCH_MSTist = objclsSubBranchMasterDAL.GetAllSubBranchFullInfo(Convert.ToInt32(ddlBranch.SelectedValue));
            }
            if (objIN_TB_SUBBRANCH_MSTist != null)
            {
                grdEmploye.DataSource = objIN_TB_SUBBRANCH_MSTist;
                grdEmploye.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdEmploye_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEmploye.PageIndex = e.NewPageIndex;
        FillSubBranch();
    }
    protected void grdEmploye_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edt")
            {
                int BranchID = Convert.ToInt32(e.CommandArgument.ToString());
                clsSubBranchMasterDAL objclsSubBranchMasterDAL = new clsSubBranchMasterDAL();
                IN_TB_SUBBRANCH_MST objIN_TB_SUBBRANCH_MST = new IN_TB_SUBBRANCH_MST();

                IN_TB_USER ObjIN_TB_USER = new IN_TB_USER();

                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                ObjIN_TB_USER = objclsUserLoginDAL.GetUserBySubBranchID(BranchID);
                if (ObjIN_TB_USER != null)
                {
                    txtSubBranchName.Text = ObjIN_TB_USER.NAME;
                    txtemail.Text = ObjIN_TB_USER.EMAIL;
                    txtmobile.Text = ObjIN_TB_USER.MOBILE;
                    txtDob.Text = Convert.ToString(ObjIN_TB_USER.DOB);
                    //txtPhone.Text= Convert.ToString(ObjIN_TB_USER)
                }

                objIN_TB_SUBBRANCH_MST = objclsSubBranchMasterDAL.GetSubBranchById(BranchID);



                if (objIN_TB_SUBBRANCH_MST != null)
                {
                    ddlBranch.SelectedValue = objIN_TB_SUBBRANCH_MST.BRANCH_ID.ToString();
                    txtSubBranchName.Text = objIN_TB_SUBBRANCH_MST.SUBBRANCH_NAME;
                    txtPhone.Text = objIN_TB_SUBBRANCH_MST.PHONE;
                    txtSubBranchDescription.Text = Convert.ToString(objIN_TB_SUBBRANCH_MST.SUBBRANCH_DESCRIPTION);
                    DateTime dtill = (DateTime)objIN_TB_SUBBRANCH_MST.DOWNLOAD_TILL;
                    txtdownloadtill.Text = dtill.ToString("dd-MMM-yyyy");
                    DateTime ltill = (DateTime)objIN_TB_SUBBRANCH_MST.LOGIN_TILL;
                    txtlogintill.Text = ltill.ToString("dd-MMM-yyyy");
                    Session["SubBranchId"] = BranchID;


                }
            }

            if (e.CommandName == "Del")
            {
                int BranchId = Convert.ToInt32(e.CommandArgument.ToString());
                clsSubBranchMasterDAL objclsSubBranchMasterDAL = new clsSubBranchMasterDAL();
                IN_TB_SUBBRANCH_MST objIN_TB_SUBBRANCH_MST = new IN_TB_SUBBRANCH_MST();
                objclsSubBranchMasterDAL.DeleteSubBranch(BranchId);
                FillSubBranch();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void FillddlBranch()
    {
        try
        {
            clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
            List<IN_TB_BRANCH_MST> objIN_TB_BRANCH_MST = new List<IN_TB_BRANCH_MST>();
            objIN_TB_BRANCH_MST = objclsBranchMasterDAL.GetAllBranch();
            ddlBranch.Items.Clear();
            ddlBranch.AppendDataBoundItems = true;
            ddlBranch.DataTextField = "BRANCH_NAME";
            ddlBranch.DataValueField = "ID";
            ddlBranch.Items.Add("Select");
            ddlBranch.Items[0].Value = "0";
            ddlBranch.DataSource = objIN_TB_BRANCH_MST;
            ddlBranch.DataBind();
            clsBranchMasterDAL obj = new clsBranchMasterDAL();
            if (clsEncryptionDecryption.Decrypt(Session["RoleId"].ToString()).Trim() == "5")
            {
                ddlBranch.Text = obj.GetBranchByUserId(Convert.ToInt32(Session["UserId"].ToString())).ID.ToString();
                ddlBranch.Enabled = false;
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdEmploye_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //Getting State Names
                    int BranchIDId = Convert.ToInt32(e.Row.Cells[1].Text);
                    clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
                    IN_TB_BRANCH_MST objIN_TB_BRANCH_MST = new IN_TB_BRANCH_MST();
                    objIN_TB_BRANCH_MST = objclsBranchMasterDAL.GetBranchById(BranchIDId);
                    e.Row.Cells[1].Text = objIN_TB_BRANCH_MST.BRANCH_NAME;

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}